package com.ace3l.legal.gwt.studentportal.mainpage.client;


import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.Composite;
import com.gwtext.client.widgets.layout.FitLayout;
import com.gwtext.client.widgets.layout.TableLayout;
import com.gwtext.client.widgets.layout.TableLayoutData;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;
import com.gwtext.client.widgets.grid.*;
import com.gwtext.client.widgets.grid.event.GridCellListenerAdapter;
import com.gwtext.client.widgets.PaddedPanel;
import com.gwtext.client.widgets.Toolbar;
import com.gwtext.client.widgets.ToolbarButton;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.core.UrlParam;
import com.gwtext.client.data.JsonReader;
import com.gwtext.client.data.Store;
import com.gwtext.client.data.Record;
import com.gwtext.client.data.RecordDef;
import com.synesthesiaconsulting.gwt.resources.client.DirectorUnit;
import com.synesthesiaconsulting.gwt.resources.client.GWTUtils;
import com.ace3l.legal.gwt.domainmodule.client.UserServiceAsync;
import com.ace3l.legal.gwt.domainmodule.client.UserService;
import com.ace3l.legal.gwt.domainmodule.client.StudentPortalServiceAsync;
import com.ace3l.legal.gwt.domainmodule.client.StudentPortalService;
import com.ace3l.legal.gwt.domainmodule.gwt.GWTStudent;

/**
 * Created by IntelliJ IDEA.
 * User: ageer
 * Date: Jul 27, 2008
 * Time: 7:12:49 PM
 * To change this template use File | Settings | File Templates.
 */
public class StudentPortalWidget extends Composite {
    com.gwtext.client.widgets.Panel portalPanel = new com.gwtext.client.widgets.Panel();
    com.gwtext.client.widgets.Panel tablePanel = new com.gwtext.client.widgets.Panel();

    Toolbar buttonBar = new Toolbar();
    DirectorUnit director;

    com.gwtext.client.widgets.Panel newsPanel = new com.gwtext.client.widgets.Panel();
    com.gwtext.client.widgets.Panel notificationPanel = new com.gwtext.client.widgets.Panel();
    com.gwtext.client.widgets.Panel newProjectsPanel = new com.gwtext.client.widgets.Panel();
    com.gwtext.client.widgets.Panel projectsPanel = new com.gwtext.client.widgets.Panel();
    final StudentPortalServiceAsync studentService = StudentPortalService.App.getInstance();

   

    public StudentPortalWidget(DirectorUnit director) {
        this.director = director;
        newsPanelInit();
        notificationPanelInit();
        projectsPanelInit();
        newProjectsPanelInit();
        toolbarInit();

        portalPanel.setBorder(true);
        portalPanel.setPaddings(10);
        portalPanel.setLayout(new FitLayout());
        portalPanel.setTopToolbar(buttonBar);

        tablePanel.setAutoScroll(true);
        tablePanel.setBorder(false);
        tablePanel.setLayout(new TableLayout(2));

        tablePanel.add(new PaddedPanel(newsPanel,0,0,5,10));
        tablePanel.add(new PaddedPanel(notificationPanel,0,5,0,10));
        tablePanel.add(new PaddedPanel(newProjectsPanel,0,0,0,10), new TableLayoutData(2));
        tablePanel.add(new PaddedPanel(projectsPanel,0,0,0,0), new TableLayoutData(2));

        portalPanel.add(tablePanel);
        initWidget(portalPanel);
    }

    private void newsPanelInit() {
        newsPanel.setWidth(464);
        newsPanel.setHeight(300);
        newsPanel.setFrame(true);
        newsPanel.setTitle("News");
        newsPanel.setCollapsible(true);
        newsPanel.setAnimCollapse(true);
        com.gwtext.client.widgets.form.Label myLabel = new com.gwtext.client.widgets.form.Label("News Portlet");
        newsPanel.add(myLabel);
    }

    private void notificationPanelInit() {
        notificationPanel.setWidth(464);
        notificationPanel.setHeight(300);
        notificationPanel.setFrame(true);
        notificationPanel.setTitle("Notifications");
        notificationPanel.setCollapsible(true);
        notificationPanel.setAnimCollapse(true);
        com.gwtext.client.widgets.form.Label myLabel = new com.gwtext.client.widgets.form.Label("Notification Portlet");
        notificationPanel.add(myLabel);
    }

    private void projectsPanelInit() {
        projectsPanel.setWidth(938);
        projectsPanel.setHeight("auto");
        projectsPanel.setFrame(true);
        projectsPanel.setTitle("My Projects");
        projectsPanel.setCollapsible(true);
        projectsPanel.setAnimCollapse(true);
        com.gwtext.client.widgets.form.Label myLabel = new com.gwtext.client.widgets.form.Label("My Projects Portlet");
        projectsPanel.add(myLabel);
    }

    private void newProjectsPanelInit() {
        newProjectsPanel.setWidth(938);
        newProjectsPanel.setHeight("auto");
        newProjectsPanel.setFrame(true);
        newProjectsPanel.setTitle("Available Projects");
        newProjectsPanel.setCollapsible(true);
        newProjectsPanel.setAnimCollapse(true);

        final RecordDef rdef = new RecordDef(GWTUtils.createFieldDefArrayFromHeader(
                new String[] {"integer_:_id",
                        "string_:_title",
                        "integer_:_hoursAllotted",
                        "date_:_dueDate_:_Y-m-d H:i",
                        "string_:_lawFirmName"}));

        final JsonReader newProjectsReader = new JsonReader(rdef);
        newProjectsReader.setRoot("list");
        final Store newProjectsStore = new Store(newProjectsReader);

        final AsyncCallback getNewProjectsCallback = new AsyncCallback() {
            public void onSuccess(Object result) {
                String jsonResult = (String)result;
                newProjectsStore.loadJsonData(jsonResult,false);


            }

            public void onFailure(Throwable caught) {
                ;
            }
        };


        final GridPanel grid = new GridPanel();
        Renderer viewLinkRender = new Renderer() {
            public String render(Object o, CellMetadata cellMetadata, Record record, int i, int i1, Store store) {
                final Integer projId = (Integer) o;
                com.google.gwt.user.client.ui.Label lbl = new com.google.gwt.user.client.ui.Label("-view-");
                lbl.addClickListener(new ClickListener() {
                    public void onClick(Widget sender) {
                        Window.alert("Request to view object " + projId);
                    }
                });
                return  lbl.getElement().getString();
            }
        };
        ColumnConfig viewColumn = new ColumnConfig("View","id",30,false, viewLinkRender);
        ColumnConfig[] columns = new ColumnConfig[] {
                viewColumn,
                new ColumnConfig("Project Title", "title", 300, true),
                new ColumnConfig("Due Date","dueDate",100, true, GWTUtils.dateRender),
                new ColumnConfig("Total Hours", "hoursAllotted", 100, true),
                new ColumnConfig("Law Firm Name", "lawFirmName", 200, true),

        };
        grid.addGridCellListener(
                new GridCellListenerAdapter() {
                    public void onCellClick(GridPanel gridPanel, int i, int i1, EventObject eventObject) {
                        if (i1 == 0) {
                            String projId = gridPanel.getStore().getRecordAt(i).getAsString("id");
                            StudentProjectViewWidget projectView = new StudentProjectViewWidget(projId);
                            com.gwtext.client.widgets.Window wProject= new com.gwtext.client.widgets.Window("Project Viewer");
                            wProject.setWidth(700);
                            wProject.setHeight(550);
                            wProject.add(projectView);
                            wProject.setCloseAction(com.gwtext.client.widgets.Window.CLOSE);
                            wProject.show();
                            Window.alert("Request to view object " + gridPanel.getStore().getRecordAt(i).getAsString("id"));
                        }
                    }
                }
        );
        grid.setColumnModel(new ColumnModel(columns));
        grid.setHeight(200);
        grid.setWidth(920);
        grid.setStore(newProjectsStore);
        studentService.getAbridgedNewProjects(getNewProjectsCallback);
        newProjectsPanel.add(grid);
    }
    private void toolbarInit() {
        final AsyncCallback logoutCallback = new AsyncCallback() {
            public void onSuccess(Object result) {
                director.applyAction("logout");
            }

            public void onFailure(Throwable caught) {
                director.applyAction("logout");
            }
        };
        ButtonListenerAdapter baLogout = new ButtonListenerAdapter() {
            public void onClick(com.gwtext.client.widgets.Button button, EventObject eventObject) {
                UserServiceAsync us = UserService.App.getInstance();
                us.logoutUser(logoutCallback);
            }
        };
        final ToolbarButton myProfile = new ToolbarButton("My Profile", new ButtonListenerAdapter() {
            public void onClick(com.gwtext.client.widgets.Button button, EventObject eventObject) {

            }
        });
        final ToolbarButton logout = new ToolbarButton("Logout", baLogout);
        AsyncCallback toolbarCallback = new AsyncCallback() {
            public void onSuccess(Object result) {
                GWTStudent me = (GWTStudent)result;
                buttonBar.addText("Welcome Back " + me.getFirstName() + " " + me.getLastName());
                buttonBar.addFill();

                buttonBar.addButton(myProfile);
                buttonBar.addSeparator();
                buttonBar.addButton(logout);
            }

            public void onFailure(Throwable caught) {
                buttonBar.addText("Welcome Back You.");
                buttonBar.addFill();

                buttonBar.addButton(myProfile);
                buttonBar.addSeparator();
                buttonBar.addButton(logout);
            }
        };
        studentService.getAbridgedStudent(toolbarCallback);
    }
}
